package org.barad.voiceuserinterface.widgets;

import org.apache.log4j.Logger;
import org.barad.voiceuserinterface.SpeechProcessingManager;
import static org.barad.voiceuserinterface.VoiceInterfaceProperties.DEBUG;
import static org.barad.voiceuserinterface.VoiceInterfaceProperties.INTERMEDIATE_INTERACTION_PAUSE;

public class Text extends Widget {
	private Logger log = Logger.getLogger(getClass());
	private static final int DEFAULT_LENGTH = 10;
	private String text;
	private int length = DEFAULT_LENGTH;
	private boolean editable;
	
	@Override
	public void interact() {
		if (DEBUG) {
			log.info("Before the interaction the value of Text " + id + " is: " + text);
		}
		if (enabled) {
			outputText("Current value is");
			if (!isEditable()) {
				SpeechProcessingManager.getInstance().speak("Please, provide value", wordsPerMinute);
				inputText();
			}
		}
		if (DEBUG) {
			log.info("After the interction the value of Text " + id + " is: " + text);
		}
	}

	public void inputText() {
		String result = SpeechProcessingManager.getInstance().listen();
		if (!isEmpty(result) && result.length() > length) {
			text = result.substring(0, length);
		} else {
			text = result;
		}
	}
	
	public void outputText(String additionalMessage) {
		String outputText = null;
		if (!isEmpty(additionalMessage)) {
			outputText = additionalMessage + " ";
		}
		if (!isEmpty(text)) {
			outputText = outputText + text;
		} 
		SpeechProcessingManager.getInstance().speak(outputText, super.wordsPerMinute);
	}

	public int getLength() {
		return length;
	}
	
	public void setLength(int length) {
		this.length = length;
	}
	
	public String getText() {
		return text;
	}
	
	public void setText(String text) {
		this.text = text;
	}

	public boolean isEditable() {
		return editable;
	}

	public void setEditable(boolean readonly) {
		this.editable = readonly;
	}
}
